
package system.base;

import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.LoginContext;

import com.hitrust.gs.util.date.DateUtil;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.ObjectList;
import framework.core.SearchEvent;
import framework.core.SrvContext;
import framework.core.StringUtil;

/**
 * 处理产品出库必须对应工单
 * @author wzh
 *
 */

public class ProductOutCategorySrv extends IHandler
{

	public ProductOutCategorySrv() 
	{
		super();
	}
	private void listProductOutCategory(DBConnection con,SrvContext context, ILoginContext loginContext,String path) throws Exception
	{
		ProductOutCategorySearchEvent event = new ProductOutCategorySearchEvent();
		ProductOutCategory poc=(ProductOutCategory)event.fromFormInitPojo(context,ProductOutCategory.class);
		event.setPageNum(poc.getPageNum());
		event.setPageSize(poc.getPageSize());
		event.setPoc(poc);
		context.setSessionValue("ProductOutCategorySearchEvent",event);
		
		context.m_req.setAttribute("outCategoryList",
				 getAllOutCategory());
		context.setNextUrl(path+"ProductOutCategoryList.jsp");
	}

	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listProductOutCategory"))
		{
			listProductOutCategory(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("exportProductOutCategory"))
        {
            exportProductOutCategory(con,context,path);
            return;
        }

		Logger.throwException("ProductOutCategorySrv","unknown action_code="+action_code);
	}
    private HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,String aValue){
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        return tCell;
    }
	   private void exportProductOutCategory(DBConnection con,SrvContext context,String path) throws Exception
	    {
	        HSSFWorkbook tWb = new HSSFWorkbook();
	        HSSFSheet tSheet = tWb.createSheet();
	        tWb.setSheetName(0,"均摊产品信息",HSSFCell.ENCODING_UTF_16);
	        HSSFRow tRow = tSheet.createRow((short)0);	       
	         createCell(tWb,tRow,(short)0, "产品编码");
	        createCell(tWb,tRow,(short)1, "产品名称");
	         createCell(tWb,tRow,(short)2,"规格");
	         createCell(tWb,tRow,(short)3,"型号");
	         createCell(tWb,tRow,(short)4,"分类");
	         createCell(tWb,tRow,(short)5,"单位");
	        
	        ProductOutCategorySearchEvent event = (ProductOutCategorySearchEvent)context.getSessionValue("ProductOutCategorySearchEvent");
	        event.setPageNum(1);
	        event.setPageSize(ConstDefine.INT_MAX);
	        
	        ProductOutCategory p= new ProductOutCategory();
	    	ObjectList list = p.search(con, event);
	        
	      
	        //------------------------
	        int j = 0;
	        for(int i=0;i<list.size();i++)
	        {
	        	ProductOutCategory  obj = (ProductOutCategory )list.get(i);
	            Product head = Product.fetch(con,
						obj.getProductId());
	            if (head != null) {
	                HSSFRow row = tSheet.createRow((short)++j);
	                 createCell(tWb,row,(short)0, head.getConcentricityCode());
	                createCell(tWb,row,(short)1, head.getName());
	                 createCell(tWb,row,(short)2, head.getSpec());
	                createCell(tWb,row,(short)3, head.getModel());
	                createCell(tWb,row,(short)4, obj.getOutCategory());
	                createCell(tWb,row,(short)5, head.getProductUnit());
	            }
	        }
	        

        
	        String fileName = "Product_out_category.xls";
	        context.m_res.setContentType("application/ms-excel");
	        context.m_res.addHeader("Content-disposition", "filename="+fileName);
	        ServletOutputStream out = context.getServletOutputStream();
	        tWb.write(out);
	        out.flush();
	        out.close();
	    }

	
    /**
     * 取得所有固定工单
     * 
     * @return
     */
    public ArrayList getAllOutCategory() {
    	ArrayList tResult = new ArrayList();
		String[] tArray = new String[2];
		tArray[0] = "01310010001";
		tArray[1] = "通用件";
		tResult.add(tArray);
		tArray = new String[2];
		tArray[0] = "01310010002";
		tArray[1] = "总成组领用";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010003";
        tArray[1] = "油料领用";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010004";
        tArray[1] = "车身组领用";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010005";
        tArray[1] = "肇事组领用";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010006";
        tArray[1] = "车身高保";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010007";
        tArray[1] = "轮胎";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010008";
        tArray[1] = "电瓶";
		tResult.add(tArray);
		tArray = new String[2];
        tArray[0] = "01310010009";
        tArray[1] = "外修费用";
		tResult.add(tArray);
        return tResult;
    }
    
}
